<template>
  <div class="mod-user">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.name" placeholder="产品名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-select v-model="dataForm.type" placeholder="分类" clearable>
          <el-option value="1" label="秒下款">秒下款</el-option>
          <el-option value="2" label="无门槛">无门槛</el-option>
          <el-option value="3" label="低息贷款">低息贷款</el-option>
          <el-option value="4" label="热门推荐">热门推荐</el-option>
          <el-option value="5" label="高通过">高通过</el-option>
        </el-select>
      </el-form-item>
      <!-- <el-select v-model="dataForm.marketId" placeholder="所属贷超">
        <el-option v-for="market in marketList" :key="market.id" :label="market.id">
          {{ market.name }}
        </el-option>
      </el-select> -->
      <el-form-item>
        <el-select v-model="dataForm.status" placeholder="状态" clearable>
          <el-option value="1" label="上线">上线</el-option>
          <el-option value="2" label="下线">下线</el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button v-if="isAuth('api:product:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button v-if="isAuth('api:product:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      :header-cell-style="tableHeaderColor"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="id"
        header-align="center"
        align="center"
        width="50"
        label="ID">
      </el-table-column>
      <el-table-column
        prop="name"
        header-align="center"
        fixed="left"
        align="center"
        width="100"
        label="产品名称">
      </el-table-column>
      <el-table-column
        prop="productRemarks"
        header-align="center"
        align="center"
        width="100"
        label="产品备注">
      </el-table-column>
      <el-table-column
        prop="typeName"
        header-align="center"
        align="center"
        label="分类">
      </el-table-column>
      <!-- <el-table-column
        prop="type"
        header-align="center"
        align="center"
        width="120"
        label="分类">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.type === 1" size="small" >秒下款</el-tag>
          <el-tag v-if="scope.row.type === 2" size="small" >无门槛</el-tag>
          <el-tag v-if="scope.row.type === 3" size="small" >低息贷款</el-tag>
          <el-tag v-if="scope.row.type === 4" size="small" >热门推荐</el-tag>
          <el-tag v-if="scope.row.type === 5" size="small" >高通过</el-tag>
        </template>
      </el-table-column> -->
      <el-table-column
        prop="marketIds"
        header-align="center"
        align="center"
        width="120"
        label="所属超市">
        <template slot-scope="scope">
          <el-tag v-for="market in scope.row.markets" :key="market.id" size="small" >{{ market.name }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="logo" header-align="center" align="center" width="120" label="logo">
        <template   slot-scope="scope">            
          <img :src="scope.row.logo" width="50" height="50" />
        </template>
      </el-table-column>
      <!-- <el-table-column
        prop="number"
        header-align="center"
        align="center"
        label="人数">
      </el-table-column> -->
      <el-table-column
        prop="minAmount"
        header-align="center"
        align="center"
        label="最小金额">
      </el-table-column>
      <el-table-column
        prop="maxAmount"
        header-align="center"
        align="center"
        label="最大金额">
      </el-table-column>
      <el-table-column
        prop="rateType"
        header-align="center"
        align="center"
        label="利率类型">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.rateType === 1" size="small" type="danger">日利率</el-tag>
          <el-tag v-else size="small">月利率</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="rate"
        header-align="center"
        align="center"
        label="利率">
      </el-table-column>
      <el-table-column
        prop="minDay"
        header-align="center"
        align="center"
        width="110"
        label="贷款最小天数">
      </el-table-column>
      <el-table-column
        prop="maxDay"
        header-align="center"
        align="center"
        width="110"
        label="贷款最大天数">
      </el-table-column>
      <el-table-column
        prop="requirement"
        header-align="center"
        align="center"
        width="110"
        label="申请条件">
      </el-table-column>
      <el-table-column
        prop="needData"
        header-align="center"
        align="center"
        width="250"
        label="所需资料">
      </el-table-column>
      <el-table-column
        prop="loanTime"
        header-align="center"
        align="center"
        label="最快放款">
      </el-table-column>
      <el-table-column
        prop="auditMethod"
        header-align="center"
        align="center"
        width="250"
        label="审核方式">
      </el-table-column>
      <el-table-column
        prop="authInfo"
        header-align="center"
        align="center"
        width="250"
        label="认证信息">
      </el-table-column>
      <el-table-column
        prop="status"
        header-align="center"
        align="center"
        label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 1" size="small" >上线</el-tag>
          <el-tag v-else size="small" type="danger">下线</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="label"
        header-align="center"
        align="center"
        label="标签">
      </el-table-column>
      <el-table-column
        prop="url"
        header-align="center"
        align="center"
        width="350"
        label="产品链接">
      </el-table-column>
      <el-table-column
        prop="productDesc"
        header-align="center"
        align="center"
        width="350"
        label="产品描述">
      </el-table-column>
      <el-table-column
        prop="weight"
        header-align="center"
        align="center"
        label="权重">
      </el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        width="180"
        label="创建时间">
      </el-table-column>
      <el-table-column
        prop="updateTime"
        header-align="center"
        align="center"
        width="180"
        label="最后修改时间">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button v-if="isAuth('api:product:update')" type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button v-if="isAuth('api:product:delete')" type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
          <el-button v-if="isAuth('api:product:change')" type="text" size="small" @click="changeStatus(scope.row.id,scope.row.status)">{{scope.row.status == 1?'下线':'上线'}}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './product-add-or-update'
  export default {
    data () {
      return {
        dataForm: {
          name: '',
          marketList: [],
          status: '',
          type: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    components: {
      AddOrUpdate
    },
    activated () {
      this.getDataList()
    },
    created () {
      this.$http({
        url: this.$http.adornUrl('/api/market/select'),
        method: 'get',
        params: this.$http.adornParams()
      }).then(({data}) => {
        this.marketList = data && data.code === 0 ? data.list : []
      })
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/api/product/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'name': this.dataForm.name,
            'status': this.dataForm.status,
            'type': this.dataForm.type
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle (id) {
        var userIds = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`确定进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/api/product/delete'),
            method: 'post',
            data: this.$http.adornData(userIds, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }).catch(() => {})
      },
      //  上线或者下线
      changeStatus (pid, status) {
        var state = status === 1 ? 2 : 1
        this.$http({
          url: this.$http.adornUrl('/api/product/update/status'),
          method: 'post',
          data: this.$http.adornData(
            {
              productId: pid,
              status: state
            }, false)
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      },
      tableHeaderColor({ row, column, rowIndex, columnIndex }) {
        if (rowIndex === 0) {
          return 'color: #555;font-weight: 500;'
        }
      }

    }
  }
</script>
<style>
.el-tag + .el-tag {
    margin-left: 5px;
    margin-top: 5px;
    margin-right: 5px;
  }
</style>